#!/usr/bin
# -*- coding: utf-8 -*-

import fe_objects as feo


# ------------------------------------------------------------------------------
def readBlankLine(line, iLine, fileType, args):
    pass

# ------------------------------------------------------------------------------
def readComment(line, iLine, fileType, args):
    pass

# ------------------------------------------------------------------------------
def readNode(line, iLine, fileType, args):
    a = feo.createNode(line, iLine)
    a.read(line, iLine, fileType)
    args["ndsU"].append(a)

# ------------------------------------------------------------------------------
def readElement(line, iLine, fileType, args):
    a = feo.createElement(line, iLine)
    line = line[1:] # the type of element has been dealt with: not useful anymore
    a.read(line, iLine, fileType)
    args["elsU"].append(a)

# ------------------------------------------------------------------------------
def readDomain(line, iLine, fileType, args):
    a = feo.createDomain(line, iLine)
    a.read(line, iLine, fileType)
    args["domsU"].append(a)

# ------------------------------------------------------------------------------
def readTopologicDomain(line, iLine, fileType, args):
    a = feo.createTopologicDomain(line, iLine)
    a.read(line, iLine, fileType)
    args["topDomsU"].append(a)


# ------------------------------------------------------------------------------
def readSection(line, iLine, fileType, args):
    a = feo.createSection(line, iLine)
    line = line[1:]
    a.read(line, iLine, fileType)
    args["secsU"].append(a)

# ------------------------------------------------------------------------------
def readMaterial(line, iLine, fileType, args):
    a = feo.createMaterial(line, iLine)
    line = line[1:]
    a.read(line, iLine, fileType)
    args["matsU"].append(a)

# ------------------------------------------------------------------------------
def readBoundaryCondition(line, iLine, fileType, args):
    a = feo.createBoundaryCondition(line, iLine)
    a.read(line, iLine, fileType)
    args["bcsU"].append(a)

# ------------------------------------------------------------------------------
def readLoadCondition(line, iLine, fileType, args):
    a = feo.createLoadCondition(line, iLine)
    a.read(line, iLine, fileType)
    args["lcsU"].append(a)

# ------------------------------------------------------------------------------
parsingFunctions = {"": readBlankLine,
                    "#": readComment,
                    "n": readNode,
                    "e": readElement,
                    "d": readDomain,
                    "td": readTopologicDomain,
                    "s": readSection,
                    "m": readMaterial,
                    "bc": readBoundaryCondition,
                    "lc": readLoadCondition}

# ------------------------------------------------------------------------------
def readFile(fileName, fileType, args):
    #TODO: code the reading method for each kind of fileType
    try:
        myFile = open(fileName, "r")
    except:
        raise InputError, "Can not open " + fileName

    iLine = 0
    while True:
        line = myFile.readline()
        if not line:
            break

        iLine += 1
        commentLine = False

        line = line.replace("\n", "")
        line = line.replace("\r", "")
        line = line.replace(" ", "")
        if len(line) > 0:
            if line[0] == "#":
                commentLine = True
        line = line.split(",")

        if not commentLine:
            key = line[0].lower()
        else:
            key = "#"

        if not key in parsingFunctions.keys():
            raise ValueError, "Line " + str(iLine) + " must start with " + \
                              str(parsingFunctions.keys())
        else:
            line = line[1:]
            parsingFunctions[key](line, iLine, fileType, args)

    myFile.close()



# ==============================================================================
def test():
    """ Creates a dummy file and reads it to test the module """
    pass

if __name__ == "__main__":
    import doctest as dt
    dt.testmod(test)
